<template>
	<view class="content">
		<uni-notice-bar single text="请随意填写以下两个输入框后点计算"></uni-notice-bar>
        <uni-forms class="uform">
			<uni-forms-item label="单价:" name="name">
				<uni-easyinput type="digit" v-model="price" placeholder="单价(元)" />
			</uni-forms-item>
			<uni-forms-item label="数量:" name="name">
				<uni-easyinput type="digit" v-model="number" placeholder="数量(斤/两)" />
			</uni-forms-item>
			<uni-forms-item label="总价:" name="name">
				<uni-easyinput type="digit" v-model="total_price" placeholder="总价(元)" />
			</uni-forms-item>
		</uni-forms>
        <view class="buttonbottom">
            <view class="center-item">
            <button type="primary" class="btn bt1" @click="submitForm"><uni-icons color="#fff" type="calendar-filled" size="20"></uni-icons>计算</button>
    		<button type="warn" class="btn bt2" @click="clearall"><uni-icons color="#fff" type="close" size="20"></uni-icons>清空</button>
            </view>
            
        </view>
        <view class="fr">
            <picker :range="pointarr" :value="keyindex" @change="bindDateChange">
				<view class="uni-input">
                <text >{{pointarr[keyindex]}}</text>
                <uni-icons type="right" size="15"></uni-icons>
                </view>
			</picker>
        </view>
		
	</view>
</template>

<script>
var cachename='pointindex';
	export default {
		data() {
			return {
			    keyindex:0, 
			    pointarr:['2位小数','3位小数','4位小数','5位小数','6位小数','7位小数','8位小数','9位小数','10位小数'],
				price: '',
				number: '',
				total_price: '',
			}
		},
		onLoad() {
            var keyindex = uni.getStorageSync(cachename);
            if(keyindex)this.keyindex=keyindex;
            var mcscache = uni.getStorageSync('mcscache');
            if(!this.is_empty(mcscache)){
                if(!this.is_empty(mcscache.danjia)){this.price =mcscache.danjia;}
                if(!this.is_empty(mcscache.shuliang)){this.number=mcscache.shuliang;}
                if(!this.is_empty(mcscache.zongjia)){this.total_price=mcscache.zongjia;}
            }
		},
		methods: {
            formatnum(num){
            	if(num){ 
            		return parseFloat(num);
            	}else return 0;
            },
		    submitForm(){
		        var danjia_val = this.formatnum(this.price);
            	var shuliang_val = this.formatnum(this.number);
            	var zongjia_val = this.formatnum(this.total_price);
            	if(danjia_val==0 && shuliang_val==0 && zongjia_val==0){
            	   uni.showToast({title:'请填写单价,数量和总价中任意两个',icon:'none'});return;
            	}	
            	if(danjia_val>0 && shuliang_val>0 && zongjia_val>0){
            	   uni.showToast({title:'单价,数量和总价请留空其中一个',icon:'none'});return;
            	}	
            	if(danjia_val==0 && shuliang_val==0){
            	   uni.showToast({title:'单价,数量和总价请留空其中一个',icon:'none'});return;
            	}
            	if(danjia_val==0 && zongjia_val==0){
            	   uni.showToast({title:'单价和总价不能同时为空',icon:'none'});return;
            	}	
            	if(shuliang_val==0 && zongjia_val==0){
            	   uni.showToast({title:'数量和总价不能同时为空',icon:'none'});return;
            	}
                var mcscache = new Object;
            	if(danjia_val>0)mcscache.danjia=danjia_val;
            	if(shuliang_val>0)mcscache.shuliang=shuliang_val;
            	if(zongjia_val>0)mcscache.zongjia=zongjia_val;
                if(!this.is_empty(mcscache)){uni.setStorageSync('mcscache',mcscache);}
            	
            	if(danjia_val==0){
            		this.price =this.format_money(zongjia_val/shuliang_val);
            	}
            	if(shuliang_val==0){
            		this.number=this.format_money(zongjia_val/danjia_val);
            	}
            	if(zongjia_val==0){
            		this.total_price=this.format_money(danjia_val*shuliang_val);
            	}
		    },
            format_money(num,type=1){
                if(!this.keyindex)this.keyindex=0;
                var length = this.keyindex+2;
                var fixedNum = num.toFixed(length);
                if(type==1)fixedNum = parseFloat(fixedNum);
                return fixedNum;
            },
            is_empty(obj){
                if (!obj || obj=='') return true; 
                if (typeof obj === "string") {
                  obj = obj.replace(/\s*/g, "");//移除字符串中所有 ''
                  if (obj === '') return true;
                } 
                return (Array.isArray(obj) && obj.length === 0) || (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0);
            },
            bindDateChange(e){
                this.keyindex = e.detail.value;
                uni.setStorageSync(cachename,this.keyindex);
            },  
            clearall(){
                this.price='';
                this.number='';
                this.total_price='';
            },
		}
	}
</script>

<style>
.content {display: flex;flex-direction: column;align-items: center;justify-content: center;font-size:40rpx;}
.uform {width:95%;}
.buttonbottom{display: grid; align-items:center;width:95%;  }
.center-item {display:flex;justify-content: center;}
.center-item .btn{margin:0 5rpx;line-height:80rpx;}
.fr{width:95%;margin-top:20rpx}
.uni-input{display: flex;align-items:center;border:1px solid #ddd;padding:10rpx;border-radius:6rpx;width:180rpx;margin:0 auto;font-size:32rpx;justify-content:space-between;}
</style>
